using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace HEFramework
{
    /// <summary>
    ///
    /// UI按钮行为基类
    /// 
    /// ----------------------------------------
    /// 作者：pan
    /// 创建时间：2022-05-13 21:16:36
    /// ----------------------------------------
    /// </summary>
    [RequireComponent(typeof(Button))]
    public class UIButtonBehaviourBase : MonoBehaviour, IUIButtonBehaviour
    {
        /// <summary>
        /// 按钮
        /// </summary>
        protected Button button;

        /// <summary>
        /// 按钮进入事件
        /// </summary>
        protected Action enterEvent;

        /// <summary>
        /// 按钮按下事件
        /// </summary>
        protected Action downEvent;

        /// <summary>
        /// 按钮按下事件
        /// </summary>
        protected Action upEvent;

        /// <summary>
        /// 按钮离开事件
        /// </summary>
        protected Action exitEvent;

        /// <summary>
        /// 是否激活
        /// </summary>
        private bool enabled;

        public Button Button => button;


        /// <summary>
        /// 按钮初始化
        /// </summary>
        public void Awake()
        {
            if (button == null)
            {
                button = GetComponent<Button>();
            }

            Enable(true);
        }

        /// <summary>
        /// 返回组件
        /// </summary>
        /// <returns></returns>
        public Transform GetTransform()
        {
            return button.transform;
        }

        /// <summary>
        /// 返回对象
        /// </summary>
        /// <returns></returns>
        public GameObject GetGameObject()
        {
            return button.gameObject;
        }

        /// <summary>
        /// 进入触发
        /// </summary>
        public void OnEnter()
        {
            if (!enabled)
            {
                return;
            }

            enterEvent?.Invoke();
        }

        /// <summary>
        /// 点击触发
        /// </summary>
        public void OnDown()
        {
            if (!enabled)
            {
                return;
            }

            downEvent?.Invoke();
        }

        /// <summary>
        /// 抬起触发
        /// </summary>
        public void OnUp()
        {
            if (!enabled)
            {
                return;
            }

            upEvent?.Invoke();
        }

        /// <summary>
        /// 离开触发
        /// </summary>
        public void OnExit()
        {
            if (!enabled)
            {
                return;
            }

            exitEvent?.Invoke();
        }

        /// <summary>
        /// 是否激活
        /// </summary>
        /// <param name="_isEnable"></param>	
        public void Enable(bool _isEnable)
        {
            enabled = _isEnable;
            if (button != null)
            {
                button.interactable = _isEnable;
            }
        }

        /// <summary>
        /// 鼠标进入触发
        /// </summary>
        /// <param name="_eventData"></param>
        public void OnPointerEnter(PointerEventData _eventData)
        {
            OnEnter();
        }

        /// <summary>
        /// 鼠标按下触发
        /// </summary>
        /// <param name="_eventData"></param>
        public void OnPointerDown(PointerEventData _eventData)
        {
            OnDown();
        }

        /// <summary>
        /// 鼠标抬起触发
        /// </summary>
        /// <param name="_eventData"></param>
        public void OnPointerUp(PointerEventData _eventData)
        {
            OnUp();
        }

        /// <summary>
        /// 鼠标离开触发
        /// </summary>
        /// <param name="_eventData"></param>
        public void OnPointerExit(PointerEventData _eventData)
        {
            OnExit();
        }


        /// <summary>
        /// 注册按钮进入事件
        /// </summary>
        /// <param name="_callback"></param>
        public IUIButtonBehaviour RegisterEnterEvent(Action _callback)
        {
            enterEvent = _callback;
            return this;
        }

        /// <summary>
        /// 注册按钮按下事件
        /// </summary>
        /// <param name="_callback"></param>
        public IUIButtonBehaviour RegisterDownEvent(Action _callback)
        {
            downEvent = _callback;
            return this;
        }


        /// <summary>
        /// 注册按钮按下事件
        /// </summary>
        /// <param name="_callback"></param>
        public IUIButtonBehaviour RegisterUpEvent(Action _callback)
        {
            upEvent = _callback;
            return this;
        }

        /// <summary>
        /// 注册按钮离开事件
        /// </summary>
        /// <param name="_callback"></param>
        public IUIButtonBehaviour RegisterExitEvent(Action _callback)
        {
            exitEvent = _callback;
            return this;
        }
    }
}